package org.budo.support.javassist.util;

import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

/**
 * @author limin
 */
public class CtMethodUtil {
    public static final String $_PARAMETER_PREFIX = "$";

    public static String returnNullStatement(String returnType) {
        if ("void".equals(returnType)) {
            return "";
        } else if ("int".equals(returnType)) {
            return " return 0; ";
        } else if ("boolean".equals(returnType)) {
            return " return false; ";
        } else if ("char".equals(returnType)) {
            return " return '\\\u0000'; ";
        } else if ("double".equals(returnType)) {
            return " return 0.0d; ";
        } else if ("float".equals(returnType)) {
            return " return 0.0f; ";
        } else if ("long".equals(returnType)) {
            return " return 0L; ";
        } else if ("short".equals(returnType)) {
            return " return (short)0; ";
        } else if ("byte".equals(returnType)) {
            return " return (byte)0; ";
        } else {
            return " return null; ";
        }
    }

    public static CtClass[] getExceptionTypes(CtMethod ctMethod) {
        try {
            return ctMethod.getExceptionTypes();
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成抛出异常代码
     */
    public static String getExceptions(CtMethod ctMethod) { //组装方法的Exception声明  
        StringBuffer exceptionBuffer = new StringBuffer();
        CtClass[] exceptionTypes = CtMethodUtil.getExceptionTypes(ctMethod);
        if (exceptionTypes.length > 0)
            exceptionBuffer.append(" throws ");
        for (int i = 0; i < exceptionTypes.length; i++) {
            if (i != exceptionTypes.length - 1) {
                exceptionBuffer.append(exceptionTypes[i].getName()).append(",");
            } else {
                exceptionBuffer.append(exceptionTypes[i].getName());
            }
        }
        return exceptionBuffer.toString();
    }

    public static CtClass[] getParameterTypes(CtMethod ctMethod) {
        try {
            return ctMethod.getParameterTypes();
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getParameters(CtMethod ctMethod) {
        StringBuffer parameterBuffer = new StringBuffer();
        CtClass[] parameters = CtMethodUtil.getParameterTypes(ctMethod);
        for (int i = 0; i < parameters.length; i++) {
            CtClass parameter = parameters[i];
            String parameterType = parameter.getName();
            String refName = $_PARAMETER_PREFIX + (i + 1); // 动态指定方法参数的变量名
            if (i != parameters.length - 1)
                parameterBuffer.append(parameterType).append(" " + refName).append(",");
            else
                parameterBuffer.append(parameterType).append(" " + refName);
        }
        return parameterBuffer.toString();
    }

    public static CtClass getReturnType(CtMethod ctMethod) {
        try {
            return ctMethod.getReturnType();
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}